﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Management;
using System.Text.RegularExpressions;

namespace Future.Management
{
    public class Environment
    {
        public static string GetLocalIPAddress()
        {
            string pattern = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
            string strmac = "";
            string strip = "";
            ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection queryCollection = query.Get();
            foreach (ManagementObject mo in queryCollection)
            {
                if (mo["IPEnabled"].ToString() == "True")
                {
                    strip = (mo["IPAddress"] as String[])[0];
                    strmac = mo["MacAddress"].ToString();

                    if (Regex.IsMatch(strip, pattern) == true)
                    {
                        break;
                    }
                }
            }
            return strip;
        }
    }
}
